package dao;

import java.sql.*;
import java.util.*;

import bean.Company;
import util.DBUtil;
import util.YyndUtil;

public class CompanyDao {
	PreparedStatement pstmt = null;
	DBUtil db=new DBUtil();
	Connection conn=db.open();    
	ResultSet rs=null;
	ArrayList<Company> list=new ArrayList<Company>();
	//查询所有企业
	public ArrayList<Company> getAllCompany(){	
		String FIND="select * from company";
		try {
			pstmt=conn.prepareStatement(FIND);
			rs=pstmt.executeQuery();
			while(rs.next()){
				String Comid=rs.getString(1);
				String comName=rs.getString(2);
				String classes=rs.getString(3);
				String scale=rs.getString(4);
				String attention=rs.getString(5);
				String adapter=rs.getString(6);
				String area=rs.getString(7);
				 Company company=new Company();
				 
				 company.setCompany_id(Comid);
				 company.setCompanyName(comName);
				 company.setClasses(classes);
				 company.setScale(scale);
				 company.setAttention(attention);
				 company.setAdapter(adapter);
				 company.setArea(area);
				 
				 list.add(company);
				 
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			db.close();
		}
              return list;             
	}
	//通过企业名称模糊查询企业信息
	public ArrayList<Company> findByName(String CompanyName){		
		String FINDBYNAME="select * from company where company_name like ?";
		try {
			
			pstmt=conn.prepareStatement(FINDBYNAME);
			pstmt.setString(1, "%"+CompanyName+"%");
			rs=pstmt.executeQuery();
			while(rs.next()){
				String Comid=rs.getString(1);
				String comName=rs.getString(2);
				String classes=rs.getString(3);
				String scale=rs.getString(4);
				String attention=rs.getString(5);
				String adapter=rs.getString(6);
				String area=rs.getString(7);
				 Company company=new Company();
				 
				 company.setCompany_id(Comid);
				 company.setCompanyName(comName);
				 company.setClasses(classes);
				 company.setScale(scale);
				 company.setAttention(attention);
				 company.setAdapter(adapter);
				 company.setArea(area);

				 list.add(company);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("数据库连接失败");
		}finally{
			db.close();
		}
		return list;
	}
	//通过ID查询企业信息
	public ArrayList<Company> findById(String Comid){
		String FINDBYID="select company_name,company_classes,company_scale,company_attention,company_adapter,area from company where company_id='"+Comid+"'";
		 try {
			pstmt=conn.prepareStatement(FINDBYID);
			rs=pstmt.executeQuery();
			while(rs.next()){				
				String comName=rs.getString(1);
				String classes=rs.getString(2);
				String scale=rs.getString(3);
				String attention=rs.getString(4);
				String adapter=rs.getString(5);
				String area=rs.getString(6);
				
				 Company company=new Company();
				 
				 company.setCompany_id(Comid);
				 company.setCompanyName(comName);
				 company.setClasses(classes);
				 company.setScale(scale);
				 company.setAttention(attention);
				 company.setAdapter(adapter);
				 company.setArea(area);

				 list.add(company);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			db.close();
		}
		return list;
		
	}
	

	//添加企业
	   public void addCom(Company company){
		   String INSERT="insert into company(company_id,company_name,company_classes,company_scale,company_attention,company_adapter,area) values(?,?,?,?,?,?,?)";  
	 try {
	    	   
	    	pstmt=conn.prepareStatement(INSERT);
	    	pstmt.setString(1, YyndUtil.createId());
			pstmt.setString(2, company.getCompanyName());
			pstmt.setString(3, company.getClasses());
			pstmt.setString(4, company.getScale());
			pstmt.setString(5, company.getAttention());
			pstmt.setString(6, company.getAdapter());
			pstmt.setString(7, company.getArea());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			db.close();
		}
	   }
	   //更新用户信息
	   public void update(Company company){
		   String UPDATE="update company set company_name=?,company_classes=?,company_scale=?,company_attention=?,company_adapter=?,area=? where company_id='"+company.getCompany_id()+"'";
		   try {
			
			pstmt=conn.prepareStatement(UPDATE);
			pstmt.setString(1, company.getCompanyName());
			pstmt.setString(2, company.getClasses());
			pstmt.setString(3, company.getScale());
			pstmt.setString(4, company.getAttention());
			pstmt.setString(5, company.getAdapter());
			pstmt.setString(6, company.getArea());
			pstmt.executeUpdate();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			db.close();
		}  
	   }
	   //删除数据
	   public void delete(String Comid) throws Exception { 
		   String DELETE="delete  from company where company_id='"+Comid+"'";
		       try  
		       {  		    	  
		           pstmt=conn.prepareStatement(DELETE);
		           pstmt.executeUpdate();
		       }catch(Exception e)  
		       {  
		           System.out.println(e.toString());  
		       }finally{
		    	   db.close();
		    	   }  
   }  

}

